package com.hnkywl.sts.dao.kcgl;

import com.hnkywl.sts.entity.xsgl.Sjdj;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Property;
import org.hibernate.transform.AliasToEntityMapResultTransformer;
import org.hibernate.type.Type;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;


/**
 * Created with IntelliJ IDEA.
 * User: jerry
 * Date: 13-4-15
 * Time: 下午2:14
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class SjdjDao extends SimpleHibernateTemplate<Sjdj, Long> {
    /**
     * 查询导入的实际单价信息
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    public Page<Sjdj> findSjdjList(String carno, String fhrq, String xsdh, String htbm, Integer zt, int pageNo, int pageSize) {
        Page<Sjdj> page = new Page<Sjdj>();
        page.setAutoCount(true);
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        Criteria criteria = getSession().createCriteria(this.entityClass)
                .createAlias("cght", "cght", CriteriaSpecification.LEFT_JOIN)
                .createAlias("createUser", "createUser", CriteriaSpecification.LEFT_JOIN)
                .createAlias("shr", "shr", CriteriaSpecification.LEFT_JOIN)
                .createAlias("jtwz", "jtwz", CriteriaSpecification.LEFT_JOIN)
                .setProjection(Projections.projectionList()
                                .add(Property.forName("id").as("id"))
                                .add(Property.forName("cght.htbm").as("htbm"))
                                .add(Property.forName("cght.qdrq").as("qdrq"))
                                .add(Property.forName("jtwz.name").as("gname"))
                                .add(Property.forName("jtwz.cz").as("gcz"))
                                .add(Property.forName("jtwz.ggxh").as("ggxh"))
                                .add(Property.forName("jtwz.dw").as("gdw"))
                                .add(Projections.sqlProjection("xsdh as \"xsdh\"", new String[]{"xsdh"}, new Type[]{Hibernate.STRING}))
                                .add(Projections.sqlProjection("carno as \"carno\"", new String[]{"carno"}, new Type[]{Hibernate.STRING}))
                                .add(Projections.sqlProjection("fhrq as \"fhrq\"", new String[]{"fhrq"}, new Type[]{Hibernate.STRING}))
                                .add(Property.forName("zzl").as("zzl"))
                                .add(Property.forName("sjdj").as("sjdj"))
                                .add(Projections.sqlProjection("this_.zt as \"zt\"", new String[]{"zt"}, new Type[]{Hibernate.INTEGER}))
                                .add(Property.forName("createUser.xm").as("cxm"))
                                .add(Property.forName("dateCreated").as("crq"))
                                .add(Property.forName("shr.xm").as("shr"))
                                .add(Property.forName("shrq").as("shrq"))
                )
                .setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
        if (StringUtils.isNotEmpty(carno)) {
            criteria.add(Property.forName("carno").like(carno, MatchMode.ANYWHERE));
        }

        if (StringUtils.isNotEmpty(fhrq)) {
            criteria.add(Property.forName("fhrq").eq(fhrq));
        }

        if (StringUtils.isNotEmpty(xsdh)) {
            criteria.add(Property.forName("xsdh").like(xsdh, MatchMode.ANYWHERE));
        }

        if (StringUtils.isNotEmpty(htbm)) {
            criteria.add(Property.forName("cght.htbm").like(htbm, MatchMode.ANYWHERE));
        }

        if (null != zt) {
            criteria.add(Property.forName("zt").eq(zt));
        }

        criteria.addOrder(Property.forName("dateCreated").desc());
        criteria.addOrder(Property.forName("htbm").desc());
        Page list = this.findByCriteria(page, criteria);
        return list;
    }



    /**
     * 查询当前销售单号、车皮号及发货日期是否存在后定价
     *
     * @param xsdh
     * @param carNo
     * @param fhrq
     * @return
     */
    public List<Map> findSjdjList_sjdj(String xsdh, String carNo, String fhrq) {
        String sql = "select nvl(sj.sjdj,0) sjdj from T_BIZ_SJDJ sj where sj.xsdh = ? and sj.carno = ? and sj.fhrq = ?";
        return (List<Map>) this.createSQLQuery(sql, xsdh, carNo, fhrq).setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE).list();
    }

    //更新已经存在的后定价
    public void updateSjdj(String xsdh, String carNo, String fhrq, Double sjdj) {
        String sql = "update T_BIZ_SJDJ set sjdj = ? where xsdh = ? and carno = ? and fhrq = ?";
        this.executeSQL(sql, sjdj, xsdh, carNo, fhrq);
    }
}


